﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EDTS.Model.Weixin;
namespace EDTS.Data.Weixin
{
    public class WxMessageCommand
    {
        public static void CreateNew(WxMessageModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    model.CreatedOn = DateTime.Now;
                    model.Status = (int)WxMessageStatus.Waiting;
                    var d = Convertor.ConvertFrom<WxMessage, WxMessageModel>(model);
                    db.DataContext.WxMessages.Add(d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static WxMessageCollection Gets(WxMessageQuery sea)
        {
            var ret = new WxMessageCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<WxMessage> q = db.DataContext.WxMessages;
                    if (sea.Status > 0)
                        q = q.Where(p => p.Status == sea.Status);
                    q = q.OrderBy(p => p.ID);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<WxMessageModel, WxMessage, WxMessageCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        public static WxMessageCollection GetWaiting(int top)
        {
            var ret = new WxMessageCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<WxMessage> q = db.DataContext.WxMessages;
                q = q.Where(p => p.Status == (int)WxMessageStatus.Waiting);
                q = q.Where(p => p.SendTime.CompareTo(DateTime.Now) <= 0);
                q = q.OrderBy(p => p.ID);
                var lst = q.ToList();
                ret = Convertor.ConvertCollection<WxMessageModel, WxMessage, WxMessageCollection>(lst);
                ret.TotalRecordsCount = lst.Count;
            }
            return ret;
        }

        public static void SetResult(int id,WxMessageStatus status,string result,string errmsg=null)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.WxMessages.FirstOrDefault(p => p.ID == id);
                    if (d != null && d.Status==(int)WxMessageStatus.Waiting)
                    {
                        d.Status = (int)status;
                        d.Result = result;
                        d.ErrMsg = errmsg;
                        d.CompleteOn = DateTime.Now;
                        db.DataContext.SaveChanges();
                    }
                }
            }
        }
    }
}
